package com.kim.smartring.broadcastreceiver;

import java.util.List;
import java.util.UUID;

import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;

import com.kim.smartring.application.SmartRingApplication;
import com.kim.smartring.ble.BleNamesResolver;
import com.kim.smartring.ble.BleWrapper;
import com.kim.smartring.utils.CallSMSUtils;
import com.kim.smartring.utils.HexStringUtils;

public class IncomingSMSBroadcastReceiver extends BroadcastReceiver {

	private static final String TAG = "IncomingSMSBroadcastReceiver";

	@Override
	public void onReceive(Context context, Intent intent) {
		if (intent.getAction()
				.equals("android.provider.Telephony.SMS_RECEIVED")) {
			System.out.println(System.currentTimeMillis());
			System.out.println("broadcast begin");
			SmsManager sms = SmsManager.getDefault(); // 得到短信管理器
			Bundle bundle = intent.getExtras(); // 得到意图中的bundle对象
			if (bundle != null) {
				Object[] pdus = (Object[]) bundle.get("pdus"); // 得到名称为"pdus"的对象,是一个Object数组,里面的每一个元素为一个byte[]数组
				SmsMessage[] messages = new SmsMessage[pdus.length];
				for (int i = 0; i < pdus.length; i++)
					messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
				for (SmsMessage message : messages) {
					String msg = message.getMessageBody(); // 短信内容
					String to = message.getOriginatingAddress(); // 短信地址
					Log.d(TAG, String.format("Message Data:%s", msg));

					CallSMSUtils.sendMsgCountToRing(context, null);
					CallSMSUtils.sendMsgDetailsToRing(context, msg);
				}
			}
		}
	}

}
